//
//  PersonalViewController.m
//  gamebox_qpp
//
//  Created by 赵隆杰 on 2021/12/30.
//

#import "PersonalViewController.h"
#import "UserInfoApi.h"
#import "UserEditApi.h"
#import "PersonalTableViewCell.h"
#import "AboutViewController.h"
#import "UserLogoutApi.h"

@interface PersonalViewController ()

@property (nonatomic, strong) NSArray *cellDatas;

@end

@implementation PersonalViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.centerTitle.text = @"个人资料";
    
    self.cellDatas = [NSArray arrayWithArray:[LocalData getMenudicWithKey:@"personal_cell"]];
    
}

- (void)setupUI {
    [super setupUI];
    
    [self.view addSubview:self.tableView];
    self.tableView.estimatedRowHeight = 50;
    self.tableView.frame = CGRectMake(0, UIDevice.vg_navigationFullHeight, SL_WINDOW_WIDTH, SL_WINDOW_HEIGHT - UIDevice.vg_navigationFullHeight);
    [self.tableView registerClass:[PersonalTableViewCell class] forCellReuseIdentifier:NSStringFromClass([PersonalTableViewCell class])];
    self.tableView.tableFooterView = ({
        UIButton *view = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, SL_WINDOW_WIDTH, 50)];
        view.backgroundColor = [UIColor whiteColor];
        [view setTitle:@"退出登录" forState:UIControlStateNormal];
        [view setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        view.titleLabel.font = [UIFont systemFontOfSize:18];
        [view addTarget:self action:@selector(loginoutClick) forControlEvents:UIControlEventTouchUpInside];
        view;
    });
    
}

# pragma UITableViewDataSource, UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.cellDatas.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 10;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSArray *array = self.cellDatas[section];
    return array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *identifier = NSStringFromClass([PersonalTableViewCell class]);
    PersonalTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    NSArray *array = self.cellDatas[indexPath.section];
    NSDictionary *dictionary = array[indexPath.row];
    cell.mainStr = dictionary[@"title_str"];
    cell.cellType = [dictionary[@"cell_type"] integerValue];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray *array = self.cellDatas[indexPath.section];
    NSDictionary *dictionary = array[indexPath.row];
    PersonalCellType cellType = [dictionary[@"cell_type"] integerValue];
    if (cellType == PersonalCellType_Image) {
        return 80;
    }
    return 50;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSArray *array = self.cellDatas[indexPath.section];
    NSDictionary *dictionary = array[indexPath.row];
    PersonalCellType cellType = [dictionary[@"cell_type"] integerValue];
    if (cellType == PersonalCellType_Arrow) {
        
    } else if (cellType == PersonalCellType_Field) {
        
    } else if (cellType == PersonalCellType_Image) {
        [[ZCImageUpload shareInstance] addImageUploadBlock:^(UIImage * _Nonnull image) {
            NSString *imageStr = [NSObject UIImageToBase64Str:image];
            [LocalData saveHeadStr:imageStr];
            [self.tableView reloadRowAtIndexPath:indexPath withRowAnimation:UITableViewRowAnimationAutomatic];
        }];
    } else {
        AboutViewController *vc = [[AboutViewController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    }
}

- (void)fetchData
{
//    UserInfoApi *api = [[UserInfoApi alloc] initWithToken:[LocalData getToken]];
//    [api startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
//        if (api.success) {
//            VHWeakSelf
//            [LocalData saveUserInfoDic:api.res];
//            [self initData];
//            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"个人信息修改成功" preferredStyle:UIAlertControllerStyleAlert];
//            UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
//                self.editCompleteBlock(YES);
//                [weakSelf.navigationController popViewControllerAnimated:YES];
//            }];
//            [alertController addAction:cancelAction];
//            [self presentViewController:alertController animated:YES completion:nil];
//
//        }
//    } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
//    }];
}

- (void)initData
{
//    UserInfoModel *model = [LocalData fetchUserInfo];
//    if ([LocalData getHeadStr]) {
//        UIImage *image = [NSObject Base64StrToUIImage:[LocalData getHeadStr]];
//        self.userImage.image = image;
//    } else {
//        [self.userImage setImage:[UIImage imageNamed:@"appicon"]];
//    }
//    if (model.account.length) {
//        self.userAccountLabel.text = [NSString stringWithFormat:@"账号：%@", model.account];
//    } else {
//        if (model.nickname.length) {
//            self.userAccountLabel.text = model.nickname;
//        } else {
//            self.userAccountLabel.text = @"";
//        }
//    }
//
//    if (model.nickname.length) {
//        self.nicknameField.text = model.nickname;
//    }
//    if (model.username.length) {
//        self.mobileField.text = model.username;
//    }
}

- (void)loginoutClick
{
    VHWeakSelf
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否要退出登录" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        VHStrongSelf
        UserLogoutApi *api = [[UserLogoutApi alloc] initWithToken:[LocalData getToken]];
        [api startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
            if (api.success) {
                [LocalData clearLoginInfo];
                [self refreshAfterLogin];
                [self.navigationController popViewControllerAnimated:YES];
            }
        } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
        }];
    }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alertController addAction:defaultAction];
    [alertController addAction:cancelAction];
    [self presentViewController:alertController animated:YES completion:nil];
    
}


@end
